package com.btcode.web.core.unit;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * 地址处理工具类
 *
 * @author gislin
 */
public class URLUtil {

    private static String projectName;

    public static String getProjectName() {
        return projectName;
    }

    public static void setProjectName(String projectName) {

        if (projectName.startsWith("/")) {
            projectName = projectName.replaceFirst("/", "");
        }

        URLUtil.projectName = projectName;
    }

    public static String getURLWithoutProjectName(ServletRequest request) {
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        String uri = httpRequest.getRequestURI();
        uri = uri.replaceFirst("/" + projectName, "");
        return uri;
    }

    public static String getURL(ServletRequest request) {
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        String uri = httpRequest.getRequestURL().toString();
        return uri;
    }

    public static String prepareURL(String url) {
        if (!url.startsWith("/")) {
            url = "/" + url;
        }
        return url;
    }


    /**
     * 获取当前网络ipAddress
     * @param srequest
     * @return
     */
    public static String getIpAddr(ServletRequest srequest){
        HttpServletRequest request = (HttpServletRequest) srequest;
        String ip = request.getHeader("X-Forwarded-For");

        String ips= request.getRemoteAddr();
        if (ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)) {
            // 多次反向代理后会有多个ip值，第一个ip才是真实ip
            if( ip.indexOf(",")!=-1 ){
                ip = ip.split(",")[0];
            }
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
            System.out.println("Proxy-Client-IP ip: " + ip);
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
            System.out.println("WL-Proxy-Client-IP ip: " + ip);
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
            System.out.println("HTTP_CLIENT_IP ip: " + ip);
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
            System.out.println("HTTP_X_FORWARDED_FOR ip: " + ip);
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("X-Real-IP");
            System.out.println("X-Real-IP ip: " + ip);
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
            System.out.println("getRemoteAddr ip: " + ip);
        }
        System.out.println("获取客户端ip: " + ip);
        return ip;
    }

}